//
//  LauNavigationController.swift
//  SwiftProject
//
//  Created by Lausen on 2017/7/9.
//  Copyright © 2017年 id. All rights reserved.
//

import UIKit

class LauNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //为了解决导航条的融合,需要重新自定义一个导航条
        navigationBar.isHidden = true
    }
    
    //MARK: -- 重写pushViewController方法.实现tabBar的隐藏
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        //如果不是栈底的控制器,才需要隐藏,是栈底的控制器,不需要隐藏
        if childViewControllers.count > 0 {
            
            //隐藏控制器的底部tabBar
            viewController.hidesBottomBarWhenPushed = true
            
            var title = "返回"
            
            //判断控制器的级数,第二个控制器的leftItem的title是第一个的title.往后的都是返回
            if childViewControllers.count == 1 {
                
                title = childViewControllers.first?.title ?? "返回"
            }
            
            let vc = viewController as? LauBaseViewController
            
            vc?.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent),isBack: true)
        }
        
        //调用父类的push方法
        super.pushViewController(viewController, animated: animated)
    }
    
    @objc private func popToParent() {
        
        popViewController(animated: true)
    }
}
